package com.hzp.micromall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzp.micromall.common.core.api.CommonPage;
import com.hzp.micromall.order.constants.OrderReturnApplyStatus;
import com.hzp.micromall.order.dao.OmsOrderReturnApplyDao;
import com.hzp.micromall.order.dto.OmsOrderReturnApplyResult;
import com.hzp.micromall.order.dto.OmsReturnApplyQueryParam;
import com.hzp.micromall.order.dto.OmsUpdateStatusParam;
import com.hzp.micromall.order.entity.OmsOrderReturnApply;
import com.hzp.micromall.order.service.OmsOrderReturnApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author houzipeng
 * @Date 2022/2/28 22:09
 * @Version 1.0
 */
@Service
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyDao, OmsOrderReturnApply> implements OmsOrderReturnApplyService {

    @Autowired
    private OmsOrderReturnApplyDao orderReturnApplyDao;

    @Override
    public CommonPage<OmsOrderReturnApply> getPage(OmsReturnApplyQueryParam queryParam, Integer pageSize, Integer pageNum) {
        Page<OmsOrderReturnApply> page = new Page<>(pageNum, pageSize);
        orderReturnApplyDao.getPage(page, queryParam);
        return new CommonPage<>(page.getCurrent(), page.getTotal(), page.getSize(), page.getRecords());
    }

    @Override
    public int delete(List<Long> ids) {
        LambdaQueryWrapper<OmsOrderReturnApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OmsOrderReturnApply::getId, ids)
                .eq(OmsOrderReturnApply::getStatus, 3);
        return orderReturnApplyDao.delete(wrapper);
    }

    @Override
    public int updateStatus(Long id, OmsUpdateStatusParam statusParam) {
        Integer status = statusParam.getStatus();
        OmsOrderReturnApply returnApply = new OmsOrderReturnApply();
        if(status.equals(OrderReturnApplyStatus.CONFIRM_RETURN)){
            //确认退货
            returnApply.setId(id);
            returnApply.setStatus(status);
            returnApply.setReturnAmount(statusParam.getReturnAmount());
            returnApply.setCompanyAddressId(statusParam.getCompanyAddressId());
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else if(status.equals(OrderReturnApplyStatus.COMPLETE_RETURN)){
            //完成退货
            returnApply.setId(id);
            returnApply.setStatus(status);
            returnApply.setReceiveTime(new Date());
            returnApply.setReceiveMan(statusParam.getReceiveMan());
            returnApply.setReceiveNote(statusParam.getReceiveNote());
        }else if(status.equals(OrderReturnApplyStatus.REJECT_RETURN)){
            //拒绝退货
            returnApply.setId(id);
            returnApply.setStatus(status);
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else{
            return 0;
        }
        return orderReturnApplyDao.updateById(returnApply);
    }

    @Override
    public OmsOrderReturnApplyResult getItem(Long id) {
        return orderReturnApplyDao.getDetail(id);
    }
}
